/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "GalaxyConsumer.h"
#include "Planet.h"

#include <OgreColourValue.h>
#include <OgreEntity.h>
#include <OgreNode.h>
#include <OgreRoot.h>
#include <OgreSceneManager.h>

#include <Util/Logger.h>
#include <Modules/Gfx/Ogre/Manager.h>

namespace galaxy {
    namespace gfx {
        namespace ogre3d {
            namespace engine {
                namespace galactic {

                    const char * GalaxyConsumer::c_GalaxyConsumerType = "gfx.engine.galaxy.consumer";

                    GalaxyConsumer::GalaxyConsumer() {
                        m_Manager = (Manager *) 0L;
                    }

                    GalaxyConsumer::GalaxyConsumer (Manager& manager) {
                        m_Manager = &manager;
                    }

                    void GalaxyConsumer::handleEvent (mediator::Event& event) {
                        LOG_ERROR ("GalaxyConsumer: cannot handle POST_IMMEDIATE events, use POST_DELAYED instead");
                    }

                    bool GalaxyConsumer::handleQueuedEvent (mediator::Event& event) {
                        if (event.category() == mediator::CATEGORY_GFX) {
                            if (event.type() == MODULE_TYPE_GFX_ENTITY_COMMAND) {
                                if (event.subtype() == gfx::Entity::GFX_ENTITY_POPULATE) {
                                    populateEntities ((gfx::EntityEvent&) event);
                                }
                            }
                        }
                        return true;
                    }

                    bool GalaxyConsumer::populateEntities (gfx::EntityEvent& event) {
                        Ogre::Root * root = m_Manager->getRoot ();

                        Ogre::SceneManager *sceneManager = root->getSceneManager("Default SceneManager");

                        sceneManager->setAmbientLight( Ogre::ColourValue( 1,1,1 ) );

                        // currently ignore the input, just draw a planet and a space station.
                        Planet * planet = new Planet ();
                        planet->createSphereMesh("mySphereMesh", 100, 64, 64);

                        Ogre::Entity* planetEntity = sceneManager->createEntity("mySphereEntity", "mySphereMesh");
                        Ogre::SceneNode* planetNode = sceneManager->getRootSceneNode()->createChildSceneNode();
                        planetEntity->setMaterialName("Planet/planetMap");
                        planetNode->attachObject(planetEntity);

                        return true;
                    }

                } // namespace galactic
            } // namespace engine
        } // namespace ogre3d
    } // namespace gfx
} // namespace galaxy
